class Solution {
public:
    int dir[4][2] = {0, 1, -1, 0, 1, 0, 0, -1};
    int dfs(vector<vector<int>> &grid, int x, int y) {
        grid[x][y] = 2;
        int res = 1;
        for (int k = 0; k < 4; k++) {
            int dx = x + dir[k][0], dy = y + dir[k][1];
            if (dx < 0 || dx >= grid.size() || dy < 0 || dy >= grid[0].size()) continue;
            if (grid[dx][dy] != 1) continue;
            res += dfs(grid, dx, dy);
        }
        return res;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] != 1) continue;
                ans = max(ans, dfs(grid, i, j));
            }
        }
        return ans;
    }
};
